package org.brandonep.server.home.log5;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.brandonep.server.home.log5.data.Controller;
import org.brandonep.server.home.log5.data.CustomDatastore;
import org.brandonep.server.home.log5.data.Device;

import com.google.appengine.api.channel.ChannelPresence;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.code.twig.ObjectDatastore;

@SuppressWarnings("serial")
public class DisconnectedServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		ChannelService channelService = ChannelServiceFactory.getChannelService();
		ChannelPresence presence = channelService.parsePresence(req);
		
		ObjectDatastore datastore = new CustomDatastore();
		
		Device device = datastore.load(Device.class, presence.clientId());
		if(device != null){
			datastore.delete(device);
		}
		
		Controller controller = datastore.load(Controller.class, presence.clientId());
		if(controller != null){
			datastore.delete(controller);
		}		
	}
}
